/*
This .do file contains code that builds the community and nursing home cohorts 
used in the paper: Oh et al. (2023)

Last updated: 
*/
****************************************************************************
*For benes in the NH cohort, pull the hee_provnum, _from & _thru dates for all NH stays
use "P:\apced\shared\aim1\data_temp\adrd_nh_rhf.dta", clear

*Check how many benes in the NH cohort have multiple NH provnums to identify benes who transferred between NHs in the last year of life (don’t count provnums for non-NH stays such as hospitals, etc.)
bys bene_id_18900: egen count = nvals(hee_provn)
preserve
collapse (max) count, by(bene_id_18900)

tab count, missing
restore

*NEW: Compare max-LOS NH to last NH @ end of obs window
gsort bene_id_18900 -hee_los
bys bene_id_18900: egen longstay=max(hee_los)
g longest_NH=hee_provn if hee_los==longstay
bys bene_id_18900: replace longest_NH=longest_NH[1]

gsort bene_id_18900 -hee_thru
bys bene_id_18900: g visit = _n
g last_NH=hee_provn if visit==1
bys bene_id_18900: replace last_NH=last_NH[1]

g same_NH=0
bys bene_id_18900: replace same_NH=1 if longest_NH==last_NH

preserve
collapse (max) same_NH, by(bene_id_18900)
tab same_NH

restore


*NEW: Assign bene to last NH @ end of obs window; # of mos in fac (w/in baseline) = hee_los
by bene_id_18900: keep if _n==1
keep bene_id_18900 hee_from hee_thru hee_provn hee_los same_NH
rename hee_los baseline_los

label var hee_from "Most recent NH from date"
label var hee_thru "Most recent NH thru date"
label var hee_provn "Most recent NH provider number"
label var same_NH "Most recent NH = Longest stay NH"
label var baseline_los "Most recent NH LOS within baseline window"

count if hee_provn==""

*Add provnum, hee_from/thru dates to the bene level file
*NH provnum variables will be missing for the community cohort
save "P:\apced\shared\aim1\data_temp\adrd_nh_bene.dta", replace

merge 1:1 bene_id_18900 using "P:\apced\shared\aim1\data_analysis\adrd_ffs_cohort.dta"
*_merge==2 is community cohort (cohort==2)
*_merge==3 includes some people from transition cohort (cohort==3) who had both NH & home days
drop _merge

*Drop transitioning cohort from this file (keep NH & community cohort only) & save
drop if cohort==3

recode cohort (1=0) (2=1)
label define coh 0 "Community" 1 "NH"
label val cohort coh
label var cohort "Study cohort"
label var baseline_9mo "271 days before death"
label var baseline_1mo "31 days before death"

save "P:\apced\shared\aim1\data_temp\RHF.dta", replace

*Save a reduced form of this file that just has bene ID, date of death, & cohort category `cohort’
keep bene_id_18900 hkdod baseline_9mo baseline_1mo cohort year

save "P:\apced\shared\aim1\data_temp\cohort.dta", replace

use "P:\apced\shared\aim1\data_temp\mbsf2016.dta", clear

rename hkc_* *
rename hko_* *

foreach var of varlist alz_mid-drug_yr {
	g `var'_flag = 1 if `var'==1 | `var'==3
	replace `var'_flag = 0 if mi(`var'_flag)
}

unab dx : *_yr_flag
local dx "`dx'"
local dx : subinstr local dx "_yr_flag" "", all
di "`dx'"

g schi_mid_flag=0
g schiot_mid_flag=0
g bipl_mid_flag=0
g pvd_mid_flag=0
g ulcers_mid_flag=0
g psds_mid_flag=0
g drug_mid_flag=0

foreach var of local dx {
	g `var'= max(`var'_mid_flag, `var'_yr_flag)
}

g smi = max(schi, bipl, depr)
g smi_ot = max(schiot, bipl, depr)
g cancer = max(cabrst, cacolo, caendo, calung, capros)

keep bene_id_18900 alz-cancer
label var alz "Alzheimer's Disease"
label var alzdem "Alzheimer's Disease and Related Disorders or Senile Dementia"
label var ami "Acute Myocardial Infarction"
label var anem "Anemia"
label var asth "Asthma"
label var atrfib "Atrial Fibrillation"
label var cabrst "Breast Cancer"
label var cacolo "Colorectal Cancer"
label var caendo "Endometrial Cancer"
label var calung "Lung Cancer"
label var capros "Prostate Cancer"
label var catar "Cataract"
label var chf "Heart Failure"
label var chrkid "Chronic Kidney Disease"
label var copd "Chronic Obstructive Pulmonary Disease"
label var depr "Depression"
label var diab "Diabetes"
label var glauc "Glaucoma"
label var hipfx "Hip / Pelvic Fracture"
label var hyprlip "Hyperlipidemia"
label var bph "Benign Prostatic Hyperplasia"
label var hyperten "Hypertension"
label var hypothy "Acquired Hypothyroidism"
label var ischhd "Ischemic Heart Disease"
label var osteo "Osteoporosis"
label var arthr "Rheumatoid Arthritis / Osteoarthritis"
label var stroke "Stroke / Transient Ischemic Attack"
label var schi "Schizophrenia"
label var schiot "Schizophrenia and Other Psychotic Disorders"
label var bipl "Bipolar Disorder"
label var pvd "Peripheral Vascular Disease"
label var ulcers "Pressure and Chronic Ulcers"
label var psds "Personality Disorders"
label var drug "Drug Use Disorders"
label var smi "Serious Mental Illness (schi)"
label var smi_ot "Serious Mental Illness (schiot)"
label var cancer "Any Breast / Colorectal / Endometrial / Lung / Prostate Cancer"
g year = 2016
save "P:\apced\shared\aim1\data_temp\MBSF16.dta", replace


use "P:\apced\shared\aim1\data_temp\mbsf2017.dta", clear

rename hkc_* *
rename hko_* *

foreach var of varlist alz_mid-drug_yr {
	g `var'_flag = 1 if `var'==1 | `var'==3
	replace `var'_flag = 0 if mi(`var'_flag)
}

unab dx : *_yr_flag
local dx "`dx'"
local dx : subinstr local dx "_yr_flag" "", all
di "`dx'"

g schi_mid_flag=0
g schiot_mid_flag=0
g bipl_mid_flag=0
g pvd_mid_flag=0
g ulcers_mid_flag=0
g psds_mid_flag=0
g drug_mid_flag=0

foreach var of local dx {
	g `var'= max(`var'_mid_flag, `var'_yr_flag)
}

g smi = max(schi, bipl, depr)
g smi_ot = max(schiot, bipl, depr)
g cancer = max(cabrst, cacolo, caendo, calung, capros)

keep bene_id_18900 alz-cancer
label var alz "Alzheimer's Disease"
label var alzdem "Alzheimer's Disease and Related Disorders or Senile Dementia"
label var ami "Acute Myocardial Infarction"
label var anem "Anemia"
label var asth "Asthma"
label var atrfib "Atrial Fibrillation"
label var cabrst "Breast Cancer"
label var cacolo "Colorectal Cancer"
label var caendo "Endometrial Cancer"
label var calung "Lung Cancer"
label var capros "Prostate Cancer"
label var catar "Cataract"
label var chf "Heart Failure"
label var chrkid "Chronic Kidney Disease"
label var copd "Chronic Obstructive Pulmonary Disease"
label var depr "Depression"
label var diab "Diabetes"
label var glauc "Glaucoma"
label var hipfx "Hip / Pelvic Fracture"
label var hyprlip "Hyperlipidemia"
label var bph "Benign Prostatic Hyperplasia"
label var hyperten "Hypertension"
label var hypothy "Acquired Hypothyroidism"
label var ischhd "Ischemic Heart Disease"
label var osteo "Osteoporosis"
label var arthr "Rheumatoid Arthritis / Osteoarthritis"
label var stroke "Stroke / Transient Ischemic Attack"
label var schi "Schizophrenia"
label var schiot "Schizophrenia and Other Psychotic Disorders"
label var bipl "Bipolar Disorder"
label var pvd "Peripheral Vascular Disease"
label var ulcers "Pressure and Chronic Ulcers"
label var psds "Personality Disorders"
label var drug "Drug Use Disorders"
label var smi "Serious Mental Illness (schi)"
label var smi_ot "Serious Mental Illness (schiot)"
label var cancer "Any Breast / Colorectal / Endometrial / Lung / Prostate Cancer"
g year = 2017
save "P:\apced\shared\aim1\data_temp\MBSF17.dta", replace


use "P:\apced\shared\aim1\data_temp\mbsf2018.dta", clear

rename hkc_* *
rename hko_* *

foreach var of varlist alz_mid-drug_yr {
	g `var'_flag = 1 if `var'==1 | `var'==3
	replace `var'_flag = 0 if mi(`var'_flag)
}

unab dx : *_yr_flag
local dx "`dx'"
local dx : subinstr local dx "_yr_flag" "", all
di "`dx'"

g schi_mid_flag=0
g schiot_mid_flag=0
g bipl_mid_flag=0
g pvd_mid_flag=0
g ulcers_mid_flag=0
g psds_mid_flag=0
g drug_mid_flag=0

foreach var of local dx {
	g `var'= max(`var'_mid_flag, `var'_yr_flag)
}

g smi = max(schi, bipl, depr)
g smi_ot = max(schiot, bipl, depr)
g cancer = max(cabrst, cacolo, caendo, calung, capros)

keep bene_id_18900 alz-cancer
label var alz "Alzheimer's Disease"
label var alzdem "Alzheimer's Disease and Related Disorders or Senile Dementia"
label var ami "Acute Myocardial Infarction"
label var anem "Anemia"
label var asth "Asthma"
label var atrfib "Atrial Fibrillation"
label var cabrst "Breast Cancer"
label var cacolo "Colorectal Cancer"
label var caendo "Endometrial Cancer"
label var calung "Lung Cancer"
label var capros "Prostate Cancer"
label var catar "Cataract"
label var chf "Heart Failure"
label var chrkid "Chronic Kidney Disease"
label var copd "Chronic Obstructive Pulmonary Disease"
label var depr "Depression"
label var diab "Diabetes"
label var glauc "Glaucoma"
label var hipfx "Hip / Pelvic Fracture"
label var hyprlip "Hyperlipidemia"
label var bph "Benign Prostatic Hyperplasia"
label var hyperten "Hypertension"
label var hypothy "Acquired Hypothyroidism"
label var ischhd "Ischemic Heart Disease"
label var osteo "Osteoporosis"
label var arthr "Rheumatoid Arthritis / Osteoarthritis"
label var stroke "Stroke / Transient Ischemic Attack"
label var schi "Schizophrenia"
label var schiot "Schizophrenia and Other Psychotic Disorders"
label var bipl "Bipolar Disorder"
label var pvd "Peripheral Vascular Disease"
label var ulcers "Pressure and Chronic Ulcers"
label var psds "Personality Disorders"
label var drug "Drug Use Disorders"
label var smi "Serious Mental Illness (schi)"
label var smi_ot "Serious Mental Illness (schiot)"
label var cancer "Any Breast / Colorectal / Endometrial / Lung / Prostate Cancer"
g year = 2018
save "P:\apced\shared\aim1\data_temp\MBSF18.dta", replace


append using "P:\apced\shared\aim1\data_temp\MBSF17.dta"
append using "P:\apced\shared\aim1\data_temp\MBSF16.dta"
sort bene year
merge m:1 bene using "P:\apced\shared\aim1\data_temp\cohort.dta"
g year2 = year(hkdod)
keep if year2==year
isid bene
drop year year2 hkdod cohort baseline* _merge
save "P:\apced\shared\aim1\data_temp\MBSF.dta", replace

*Use provider specialty code to identify claims by nurse practitioners (specialty code=50), physician assistants (97), and generalist physicians (01 general practice, 08 family practice, 11 internal medicine, 12 osteopathic medicine, 38 geriatric medicine, 84 preventative medicine)	
*Please add 17 (Hospice and Palliative Care)

*Drop claims not by the above specialty codes

*Drop claims with BETOS codes other than new (M1A) and established (M1B) office visits; home (M4A) visits; and nursing home (M4B) visits. We only want evaluation & management (E/M) codes for these settings by primary care providers.

*Drop extraneous variables

*Merge m:1 to bene-level file in 1b3 `cohort' to get the cohort indicator
use "P:\apced\shared\aim1\data_temp\all_lines_new.dta", clear

merge m:1 bene_id_18900 using "P:\apced\shared\aim1\data_temp\cohort.dta"
*_merge==1 includes carrier claims not in cohort - drop
*_merge==2 is decedents with no carrier claims
drop if _merge==1
drop _merge


*For benes in the community cohort, drop claims where BETOS = M4B. (Community cohort should only be M1A, M1B, & M4A).
order bene hb* hkdod

drop if hbbetos=="M4B" & cohort==0
tab hbbetos if cohort==0

*For the nursing home cohort, drop claims where BETOS = M1A, M1B, or M4A (NH cohort should only be M4B)
drop if hbbetos != "M4B" & cohort==1
tab hbbetos if cohort==1


*Generate 3 category variable for provider type:  physician vs. NP vs. PA
label define prov 0 "Physician" 1 "NP" 2 "PA"
label val provider prov
tab provider


*Generate month variable (1-9) to indicate whether claim occurred during any of the 8 months of the observation window or last month of life
*Drop claims that occurred outside of this window
g window=.
replace window=0 if hbthru < baseline_9mo
replace window=1 if baseline_9mo <= hbthru & hbthru <= baseline_1mo
replace window=2 if baseline_1mo < hbthru  & hbthru <= hkdod
replace window=3 if hbthru > hkdod & hbthru !=.
tab window


keep if window==1 | window==2
drop window

g eightmo = hkdod - 241
g sevenmo = hkdod - 211
g sixmo = hkdod - 181
g fivemo = hkdod - 151
g fourmo = hkdod - 121
g threemo = hkdod - 91
g twomo = hkdod - 61

g month=.
replace month=0 if baseline_1mo < hbthru & hbthru <= hkdod
replace month=1 if twomo < hbthru & hbthru <= baseline_1mo
replace month=2 if threemo < hbthru & hbthru <= twomo
replace month=3 if fourmo < hbthru & hbthru <= threemo
replace month=4 if fivemo < hbthru & hbthru <= fourmo
replace month=5 if sixmo < hbthru & hbthru <= fivemo
replace month=6 if sevenmo < hbthru & hbthru <= sixmo
replace month=7 if eightmo < hbthru & hbthru <= sevenmo
replace month=8 if baseline_9mo <= hbthru & hbthru <= eightmo

label var month "Number of months before death"
drop eightmo-twomo


*Need to correct for dup rows w/ same clmID but diff provns (<0.5%)
drop hbprvtp hbplcsrv hbtaxnum
duplicates drop

gsort hbclmid -provider
bys hbclmid: g count = _n
tab count


keep if count==1


*By cohort:  Cross-tab provider type by month with row & column percentages
preserve
bys cohort provider: egen count2 = nvals(bene_id_18900)

collapse bene_id=count2 (count) hbclmid=count, by(cohort month provider)
gsort cohort provider -month
order month provider cohort hbclmid bene_id
list

g phys_claims=hbclmid if provider==0 & cohort==0
g np_claims=hbclmid if provider==1 & cohort==0
g pa_claims=hbclmid if provider==2 & cohort==0

g phys_claims_nh=hbclmid if provider==0 & cohort==1
g np_claims_nh=hbclmid if provider==1 & cohort==1
g pa_claims_nh=hbclmid if provider==2 & cohort==1

save "P:\apced\shared\aim1\data_analysis\carrier_by_month_ALL.dta", replace

restore


*By bene month (including month of death), generate each of the following measures:
*(Months with no provider visits should have a count of zero, not be missing)
*Total provider visits (restricted to above BETOS/provider codes)
*Total physician visits
*Total NP visits
*Total PA visits
*Total APC visits (3+4)
*Number of unique providers
*% of visits by physicians
*% of visits by NPs
*% of visits by PAs
*% of visits by APCs
g physician=0
g np=0
g pa=0
g apc=0
replace physician=1 if provider==0
replace np=1 if provider==1
replace pa=1 if provider==2
replace apc=1 if provider==1 | provider==2


*Reduce data set to bene month level with 9 months(rows) per bene  
*Keep bene ID, cohort category, DOD, month, and counts generated in K
bys bene_id_18900 month: egen unique_mo = nvals(hbpfnpi)
bys bene_id_18900: egen unique_bl = nvals(hbpfnpi) if month > 0
bys bene_id_18900: egen prov_unique = max(unique_bl)

collapse hkdod cohort unique_prov=unique_mo (count) prov_visits=count (sum) phys_visits=physician np_visits=np pa_visits=pa apc_visits=apc, by(bene_id_18900 month prov_unique)

g phys_pcnt = phys_visits / prov_visits
g np_pcnt = np_visits / prov_visits
g pa_pcnt = pa_visits / prov_visits
g apc_pcnt = apc_visits / prov_visits

reshape wide unique_prov prov_visits phys_visits np_visits pa_visits apc_visits phys_pcnt np_pcnt pa_pcnt apc_pcnt, i(bene_id_18900 hkdod cohort prov_unique) j(month)

foreach var of varlist unique_prov0-apc_pcnt8 {
	replace `var' = 0 if missing(`var') 
	}

reshape long unique_prov prov_visits phys_visits np_visits pa_visits apc_visits phys_pcnt np_pcnt pa_pcnt apc_pcnt, i(bene_id_18900 hkdod cohort prov_unique) j(month)

label var hkdod "Date of death"
label var cohort "Study cohort"
label var unique_prov "# unique E/M providers (month)"
label var prov_unique "# unique E/M providers (baseline)"
label var prov_visits "# E/M visits"
label var phys_visits "# visits by physician"
label var np_visits "# visits by nurse practitioner"
label var pa_visits "# visits by physicians assistant"
label var apc_visits "# visits by advanced practice clinician (NP or PA)"
label var phys_pcnt "% visits by physician"
label var np_pcnt "% visits by nurse practitioner"
label var pa_pcnt "% visits by physicians assistant"
label var apc_pcnt "% visits by advanced practice clinician (NP or PA)"


*By bene, generate counts for same measures in k across the entire 8 month baseline (excluding last month of life)
*By bene, generate a 3-category flag based on % of visits by APC for the 8 month baseline:
*Minimal or no APC involvement: <10% 
*Moderate APC involvement:  10-50% 
*Extensive APC involvement: >50% 
egen phys_total = sum(phys_visits) if month > 0, by(bene_id_18900)
egen np_total = sum(np_visits) if month > 0, by(bene_id_18900)
egen pa_total = sum(pa_visits) if month > 0, by(bene_id_18900)
egen apc_total = sum(apc_visits) if month > 0, by(bene_id_18900)

egen all_total = rowtotal(phys_total np_total pa_total)
replace all_total=. if month==0
g apc_rate = apc_total / all_total

g apc_involve=.
replace apc_involve=0 if apc_rate < 0.1
replace apc_involve=1 if apc_rate >= 0.1 & apc_rate <= 0.5
replace apc_involve=2 if apc_rate > 0.5 & apc_rate !=.
label define apc 0 "Minimal (<10%)" 1 "Limited (10-50%)" 2 "Extensive (>50%)"
label val apc_involve apc

label var phys_total "Total baseline physician visits (months 1-8)"
label var np_total "Total baseline NP visits (months 1-8)"
label var pa_total "Total baseline PA visits (months 1-8)"
label var apc_total "Total baseline APC visits (months 1-8)"
label var prov_unique "Total unique providers (months 1-8)"
label var all_total "Total E/M visits over baseline"
label var apc_rate "Proportion of APC involvement in baseline visits"
label var apc_involve "Level of APC involvement in baseline visits"
tab apc_involve if month > 0, missing

save "P:\apced\shared\aim1\data_temp\bene_month_new.dta", replace

use "P:\apced\shared\aim1\data_temp\all_lines_new.dta", clear

*Generate 3 category variable for provider type:  physician vs. NP vs. PA
label define prov 0 "Physician" 1 "NP" 2 "PA"
label val provider prov
tab provider


*Generate a facility-level APC involvement indicator
merge m:1 bene using "P:\apced\shared\aim1\data_temp\RHF.dta", keepusing(hee_provn)		
*_merge==1 includes carrier claims not in cohort - drop
*_merge==2 is decedents with no carrier claims - drop
keep if _merge==3
drop _merge
drop if hee_provn==""


*Need to correct for dup rows w/ same clmID but diff provns
drop hbprvtp hbplcsrv hbtaxnum
duplicates drop

gsort hbclmid -provider
bys hbclmid: g count = _n
bys hee_provn: egen unique = nvals(hbpfnpi)

g physician=0
g np=0
g pa=0
g apc=0
replace physician=1 if provider==0
replace np=1 if provider==1
replace pa=1 if provider==2
replace apc=1 if provider==1 | provider==2

collapse fac_unique_prov=unique (count) fac_prov_visits=count (sum) fac_phys_visits=physician fac_np_visits=np fac_pa_visits=pa fac_apc_visits=apc, by(hee_provn)

g fac_phys_pcnt = fac_phys_visits / fac_prov_visits
g fac_np_pcnt = fac_np_visits / fac_prov_visits
g fac_pa_pcnt = fac_pa_visits / fac_prov_visits
g fac_apc_pcnt = fac_apc_visits / fac_prov_visits

g fac_apc_involve=.
replace fac_apc_involve=0 if fac_apc_pcnt < 0.05
replace fac_apc_involve=1 if fac_apc_pcnt >= 0.05 & fac_apc_pcnt <= 0.5
replace fac_apc_involve=2 if fac_apc_pcnt > 0.5 & fac_apc_pcnt !=.
label define fac_apc 0 "Minimal (<5%)" 1 "Limited (5-50%)" 2 "Extensive (>50%)"		/*Per Betsy*/
label val fac_apc_involve fac_apc
label var fac_apc_involve "Facility level of APC involvement in baseline visits"
tab fac_apc_involve, missing


save "P:\apced\shared\aim1\data_temp\facility_new.dta", replace

use "P:\apced\shared\aim1\data_temp\bene_month_new.dta", clear
g year = year(hkdod)
label var year "Year of death"

merge m:1 bene using "P:\apced\shared\aim1\data_temp\MBSF.dta"
drop if _merge==2
drop _merge

merge m:1 bene using "P:\apced\shared\aim1\data_temp\RHF.dta"
drop if _merge==2
drop _merge
label val cohort coh
label var baseline_9mo "271 days before death"
label var baseline_1mo "31 days before death"
label var ccw "Count of 26 chronic conditions (no SMI)"
recode dual_dod (0=0) (1=1) (2=1)		/*Combine full & partial dual*/
recode dual_9mo (0=0) (1=1) (2=1)
rename hkzip zip5

merge m:1 hee_provn year using "P:\apced\shared\aim1\data_temp\LTCF.dta"
drop if _merge==2
drop _merge
g pct_nonwhite = 100 - pctwhite_mds3
label var pct_nonwhite "% Nonwhite in facil, MDS 3.0"

merge m:1 hee_provn year using "P:\apced\shared\aim1\data_temp\ltcf_therapy_new.dta"		
drop if _merge==2
drop _merge
label var pthrppd "OSCAR: Total PT hrs/day/resident (adjusted)"
label var othrppd "OSCAR: Total OT hrs/day/resident (adjusted)"
label var therapy_hrppd "OSCAR: Total licensed therapy (PT/OT) hrs/day/resident (adjusted)"

*Merge on bene ZIP
merge m:1 zip5 using "P:\apced\shared\common data files\rurality_by_zipcode.dta", keepusing(cnty F12424 F1406709)
drop if _merge==2
drop _merge
rename cnty res_cnty
rename F12424 res_state
rename F1406709 res_rurality
rename zip5 hkzip
rename PROV2905 zip5
label var res_cnty "FIPS state and county code"
*Merge on fac ZIP
merge m:1 zip5 using "P:\apced\shared\common data files\rurality_by_zipcode.dta", keepusing(F1406709)
drop if _merge==2
drop _merge
rename F1406709 fac_rurality
rename zip5 PROV2905

merge m:1 hee_provn using "P:\apced\shared\aim1\data_temp\facility_new.dta", keepusing(fac_apc_involve)
drop if _merge==2
drop _merge

*Merge individual ADL & CFS measures for nursing home benes (as of the 1st MDS assessment in the baseline period)
merge m:1 bene using "P:\apced\shared\aim1\data_temp\mds_cohort.dta"
drop if _merge==2
drop _merge


drop if alz==0 & alzdem==0

save "P:\apced\shared\aim1\data_analysis\bene_month_level_ALL.dta", replace


*Reduce to a bene-level file.  
*Drop month & month provider variables (Keep all the provider variables for the 8 month baseline)
drop if month==0
drop month-apc_pcnt
duplicates drop
isid bene

save "P:\apced\shared\aim1\data_analysis\bene_level_ALL.dta", replace

tab fac_apc_involve		/*For Table 2*/


*Reduce to facility-level file
collapse (mean) all_total prov_unique acute_days er_count hospc_days totbeds adm_bed pct_nonwhite paymcaid paymcare fac_rurality rnhrppd lpnhrppd cnahrppd pthrppd othrppd therapy_hrppd (max) profit, by(hee_provn fac_apc_involve)
drop if hee_provn==""
duplicates drop
isid hee_provn

save "P:\apced\shared\aim1\data_analysis\fac_level_ALL.dta", replace